package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.PmsProductAttributeDao;
import com.macro.mall.model.PmsProductAttribute;
import com.macro.mall.model.PmsSkuStock;
import com.macro.mall.service.PmsSkuStockService;
import com.macro.mall.vo.request.SkuStockResquest;
import com.macro.mall.vo.response.SkuStockResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * sku库存Controller
 */
@RestController
@Api(tags = "PmsSkuStockController", description = "sku商品库存管理")
@RequestMapping("/sku")
public class PmsSkuStockController {

    @Autowired
    private PmsSkuStockService skuStockService;

    @Autowired
    private PmsProductAttributeDao productAttributeDao;

    @ApiOperation("根据商品编号及编号模糊搜索sku库存")
    @RequestMapping(value = "/skuList", method = RequestMethod.POST)
    public CommonResult<SkuStockResponse> getList(@RequestBody SkuStockResquest resquest) {
        SkuStockResponse response = new SkuStockResponse();
        List<PmsSkuStock> skuStockList = skuStockService.getList(resquest.getPid(), resquest.getKeyword());
        List<PmsProductAttribute> attributes = productAttributeDao.getPmsProductAttribute(resquest.getCid(), 0);
        response.setSkuStockList(skuStockList);
        response.setAttributeList(attributes);
        return CommonResult.success(response);
    }

    @ApiOperation("批量更新库存信息")
    @RequestMapping(value = "/update/{pid}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long pid, @RequestBody List<PmsSkuStock> skuStockList) {
        int count = skuStockService.update(pid, skuStockList);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
